package com.pega.eprescription;

import java.util.ArrayList;

import android.R.bool;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.pega.eprescription.activity.EditDrug;
import com.pega.eprescription.database.manager.DatabaseManager;
import com.pega.eprescription.database.model.Pill;
 
public class EditPillListAdapter extends BaseAdapter  {
    
	ArrayList<Pill> allPill;
	private static LayoutInflater inflater=null;
	Pill tempValues=null;
	private boolean isEditing;
	Context activity;
	public EditPillListAdapter(Context activity, ArrayList<Pill> allPill, boolean edit) {
		this.allPill=allPill;
		
		this.isEditing=edit;
		this.activity=activity;
		inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return allPill.size();
	}
	
	@Override
	public Object getItem(int position) {
		return allPill.get(position);
	}
	
	@Override
	public long getItemId(int position) {
		return position;
	}
	
	 public static class ViewHolder{
         
         public ImageView image;
         public TextView name;
         public TextView morning;
         public TextView noon;
         public TextView night;
         public Button deleteBtn;

     }
	 
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final int  pos = position;
		ViewHolder holder = null;
		 View vi = convertView;
        
         if(convertView==null){
              
             vi = inflater.inflate(R.layout.edit_list_item, null);
              
             holder = new ViewHolder();
//             holder.image = (ImageView)vi.findViewById(R.id.img);
             holder.name=(TextView)vi.findViewById(R.id.name);
             holder.morning=(TextView)vi.findViewById(R.id.morning);
             holder.noon=(TextView)vi.findViewById(R.id.noon);
             holder.night=(TextView)vi.findViewById(R.id.night);
             holder.deleteBtn=(Button)vi.findViewById(R.id.deleteBtn);
            if(isEditing) holder.deleteBtn.setVisibility(View.VISIBLE);
            else holder.deleteBtn.setVisibility(View.GONE);
            	
             holder.deleteBtn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					DatabaseManager.getInstance(activity).deletePillRecord(allPill.get(pos));
					allPill.remove(pos);					
					notifyDataSetChanged();
					
				}
			});
             holder.image=(ImageView)vi.findViewById(R.drawable.ic_launcher);
              
             vi.setTag( holder );
         }
         else 
             holder=(ViewHolder)vi.getTag();
          
         if(allPill.size()<=0)
         {
             holder.name.setText("No Data");
              
         }
         else
         {
             tempValues=null;
             tempValues = ( Pill ) allPill.get( position );

              holder.name.setText( tempValues.getNamePill() +" ("+tempValues.getAmount()+")");
              holder.morning.setText(""+tempValues.getAmountMorning());
              holder.noon.setText(""+tempValues.getAmountNoon());
              holder.night.setText(""+tempValues.getAmountEverning());
//              holder.image.setImageResource();
//               holder.image.setImageResource(
//                           res.getIdentifier(
//                           "com.androidexample.customlistview:drawable/"+tempValues.getImage()
//                           ,null,null));

         }
         vi.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(allPill.size()>0 && isEditing)
		         {
					Pill tempV = ( Pill ) allPill.get( position );
					Intent intent = new Intent(activity, EditDrug.class);
					intent.putExtra("ID_PILL_PASS", tempV.getPillID());
					activity.startActivity(intent);
		         }
		         
			}
		});
         return vi;
	}


}